const Core = require("@alicloud/pop-core");
const config = require("../../config/config")["aliyun"];
const sysConfigServer = require("../../api/service/sys_config");

var client = new Core({
    accessKeyId: config.accessKeyId,
    accessKeySecret: config.accessKeySecret,
    endpoint: "https://dysmsapi.aliyuncs.com",
    apiVersion: "2017-05-25",
});

const sendSms = (params) => {
    let promise = new Promise(async(resolve, reject) => {
        client.request("SendSms", params, { method: "POST" }).then(
            (result) => {
                resolve(result);
                console.log(JSON.stringify(result));
            },
            (ex) => {
                console.log(ex);
                reject(ex);
            }
        );
    });

    return promise;
};
module.exports = {
    // 发送验证码
    async sendVerificationCode(phone, code) {
        let isSend = await sysConfigServer.getEnableSMS();
        if (isSend) {
            let params = {
                RegionId: "default",
                PhoneNumbers: phone,
                SignName: "玖玖商城",
                TemplateCode: "SMS_213742709",
                TemplateParam: JSON.stringify({ code }),
            };
            return await sendSms(params);
        }
        return {};
    },

    // 发送订单提醒
    async sendOrderRemind(phone, name, orderNo) {
        let params = {
            RegionId: "default",
            PhoneNumbers: phone,
            SignName: "玖玖商城",
            TemplateCode: "SMS_213692701",
            TemplateParam: JSON.stringify({ name, orderNo }),
        };
        return await sendSms(params);
    },
};